package com.tedu.ds.employee.service.impl;


import com.tedu.ds.base.enmu.EmpStatus;
import com.tedu.ds.base.exception.ServiceException;
import com.tedu.ds.base.response.StatusCode;
import com.tedu.ds.employee.mapper.EmployeeMapper;
import com.tedu.ds.employee.pojo.dto.EmployeeLoginParam;
import com.tedu.ds.employee.pojo.dto.EmployeeQuery;
import com.tedu.ds.employee.pojo.dto.EmployeeSaveParam;
import com.tedu.ds.employee.pojo.entity.Employee;
import com.tedu.ds.employee.pojo.vo.EmployeeVO;
import com.tedu.ds.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Override
    public void save(EmployeeSaveParam employeeSaveParam) {
        Employee employee=new Employee();
        BeanUtils.copyProperties(employeeSaveParam,employee);
        if (employee.getId()==null){
            employee.setPassword("123456");
            employeeMapper.insert(employee);
        }else {
            employeeMapper.update(employee);
        }
    }

    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteById(id);
    }

    @Override
    public EmployeeVO login(EmployeeLoginParam employeeLoginParam) {
        Employee employee = employeeMapper.selectByUsername(employeeLoginParam.getUsername());
        if(employee ==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!employee.getPassword().equals(employeeLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        employee.setStatus(EmpStatus.ONLINE.getCode());
        employeeMapper.update(employee);
        EmployeeVO employeeVO = new EmployeeVO();
        BeanUtils.copyProperties(employee, employeeVO);
        return employeeVO;
    }

    @Override
    public List<EmployeeVO> select(EmployeeQuery employeeQuery) {
        List<EmployeeVO> list=employeeMapper.select(employeeQuery);
        return list;
    }

    @Override
    public EmployeeVO selectByUsername(String username) {
        Employee employee=employeeMapper.selectByUsername(username);
        EmployeeVO employeeVO=new EmployeeVO();
        BeanUtils.copyProperties(employee,employeeVO);
        return employeeVO;
    }
}
